﻿using HZY.Framework.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Sunny.UI;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;

namespace WinFormsSprayProcessSCADASystem
{
    public partial class PageTotalEquipmentControl : UIPage, IScopedSelfDependency
    {
        public PageTotalEquipmentControl()
        {
            InitializeComponent();
            Globals.ServiceProvider.GetRequiredService<MainForm>().PlcValCahnge += UpdateState;
        }

        private void UpdateState()
        {
            try
            {
                device_TZ.IsActive = Convert.ToInt32(Globals.DataDic[device_TZ.CloseVariableName]) == 0 && Convert.ToInt32(Globals.DataDic[device_TZ.OpenVariableName]) == 1;
                userDeviceUnitControl3.IsActive = Convert.ToInt32(Globals.DataDic[userDeviceUnitControl3.CloseVariableName]) == 0 && Convert.ToInt32(Globals.DataDic[userDeviceUnitControl3.OpenVariableName]) == 1;
                device_CX.IsActive = Convert.ToInt32(Globals.DataDic[device_CX.CloseVariableName]) == 0 && Convert.ToInt32(Globals.DataDic[device_CX.OpenVariableName]) == 1;
                userDeviceUnitControl4.IsActive = Convert.ToInt32(Globals.DataDic[userDeviceUnitControl4.CloseVariableName]) == 0 && Convert.ToInt32(Globals.DataDic[userDeviceUnitControl4.OpenVariableName]) == 1;
                device_TH.IsActive = Convert.ToInt32(Globals.DataDic[device_TH.CloseVariableName]) == 0 && Convert.ToInt32(Globals.DataDic[device_TH.OpenVariableName]) == 1;
                userDeviceUnitControl6.IsActive = Convert.ToInt32(Globals.DataDic[userDeviceUnitControl6.CloseVariableName]) == 0 && Convert.ToInt32(Globals.DataDic[userDeviceUnitControl6.OpenVariableName]) == 1;
                userDeviceUnitControl8.IsActive = Convert.ToInt32(Globals.DataDic[userDeviceUnitControl8.CloseVariableName]) == 0 && Convert.ToInt32(Globals.DataDic[userDeviceUnitControl8.OpenVariableName]) == 1;
                userDeviceUnitControl5.IsActive = Convert.ToInt32(Globals.DataDic[userDeviceUnitControl5.CloseVariableName]) == 0 && Convert.ToInt32(Globals.DataDic[userDeviceUnitControl5.OpenVariableName]) == 1;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void PageTotalEquipmentControl_Load(object sender, EventArgs e)
        {

        }

        private void common_Btn_Control_Click(object sender, EventArgs e)
        {
            try
            {
                UISymbolButton symBtn = sender as UISymbolButton;
                if (Globals.PlcWrite(symBtn.TagString, true))
                {
                    UIMessageTip.ShowOk("写入成功");
                    ShowLog($"写入{symBtn.TagString}成功");
                }
                else
                {
                    UIMessageTip.ShowError("写入失败，请查看运行日志");
                }
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }
        }

        private void btn_DryRun_ClickEvent(object sender, EventArgs e)
        {
            try
            {
                if (Globals.PlcWrite(this.btn_DryRun.VariableName, !this.btn_DryRun.ContentButtonState))
                {
                    UIMessageTip.ShowOk("写入成功");
                    ShowLog($"写入{this.btn_DryRun.VariableName}成功");
                }
                else
                {
                    UIMessageTip.ShowError("写入失败，请查看运行日志");
                }
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }
        }

        private void device_unit_clickEvent(object sender, EventArgs e)
        {
            try
            {
                UserDeviceUnitControl symBtn = sender as UserDeviceUnitControl;
                bool IsActive = !symBtn.IsActive;
                if (Globals.PlcWrite(symBtn.OpenVariableName, !IsActive) && Globals.PlcWrite(symBtn.CloseVariableName, IsActive))
                {
                    UIMessageTip.ShowOk("写入成功");
                    ShowLog($"写入{symBtn.LabelText}" + (IsActive ? '关' : '开') + "成功");
                }
                else
                {
                    symBtn.IsActive = false;
                    UIMessageTip.ShowError("写入失败，请查看运行日志");
                }
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }
        }


        private void ShowLog(string info, LogLevel logLevel = LogLevel.Information)
        {
            this.txt_Log.AppendText($"{DateTime.Now.ToString("yyyy-mm-dd HH:mm:ss")}=>{logLevel.ToString()} {info}{Environment.NewLine}");
        }
    }
}
